package controllers;

import java.util.List;


import model.Sys_priv;

import model.User_list;

import com.et.mvc.JsonView;

/**
 * @author wzzz
 * @date 2011-3-20 下午03:27:51
 * @desc 用户登录信息表
 * 
 */
public class User_listController extends ApplicationController {
	private List l_menus = null;
	public void login() throws Exception {
	}
	// 返回信息包含：用户姓名、身份、session的信息保存user_list 
	public void handleLogin() throws Exception {
//		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("Username").trim();
		String password = request.getParameter("Password").trim();
		List<User_list> list = User_list.findBySql(User_list.class,
				"select * from user_list where user_name=? and user_password=? ", new Object[]{
			username, password});
		if(list.size()>0){
			User_list ul = list.get(0);
			if(ul!=null && ul.getUser_name()!=null){
				session.setAttribute("sessionAccount", ul);
				session.setAttribute("realName", ul.getUser_name());
				session.setAttribute("userpriv_json", ul.getUserPrivs(ul.getUser_zw()));
				redirect("/page/mainFrame.jsp"); 
			}
		}
		
//		String user_type = request.getParameter("user_type");// 员工、老师、学员
//		User_list user = User_list.authenticate(username, password, user_type);
//		if (user == null) {
//			flash.setAttribute("message", "对不起，你的帐号或密码错误");
//			flash.setAttribute("username", username);
//			flash.setAttribute("password", password);
//			redirect("/user_list/login"); // 登录失败，重新登 
//		} else {
//			session.setAttribute("sessionAccount", user);
//			session.setAttribute("user_type", user_type);
//			String real_name = "";
//			if (user_type.equals("员工")) {
//				real_name = user.getLabour(user.getUserinfo_id())
//						.getReal_name();
//			} else if (user_type.equals("教师")) {
//				real_name = user.getTeacher(user.getUserinfo_id())
//						.getTea_real_name();
//			} else { 
//				real_name = user.getStudent(user.getUserinfo_id())
//						.getStu_name();
//			}
//			session.setAttribute("realName", real_name);
//			session.setAttribute("userId", user.getUserinfo_id());
//			//
//		
//			List hm = (List) user.getUserPrivs(user.getPriv_id());
//			// 保存菜单和角色功能
//			session.setAttribute("userpriv_json", hm);
//			//
//			redirect("/page/mainFrame.jsp"); // 登录成功，重定向到首页
//		}
	}

	public void register() {
	}

	/**
	 * 创建新帐 * @throws java.lang.Exception
	 */
	public JsonView save() throws Exception {
		User_list user = User_list.createModel(User_list.class, request
				.getParameterMap());
		if (user.save()) {
			session.setAttribute("sessionAccount", user);
			JsonView view = new JsonView("success:true");
			view.setContentType("text/html;charset=utf-8");
			return view;
		} else {
			flash.setAttribute("user", user);
			JsonView view = new JsonView("success:false");
			view.setContentType("text/html;charset=utf-8");
			return view;
		}
	}



	public JsonView getUser(int id) throws Exception {
		User_list users = User_list.find(User_list.class, id);
		return new JsonView(users);
	}

	/**
	 * 保存修改的账户
	 */
	public JsonView update(int id) throws Exception {
		User_list user = User_list.createModel(User_list.class, request
				.getParameterMap());
		if (user.update()) {
			session.setAttribute("sessionAccount", user);
			JsonView view = new JsonView("success:true");
			view.setContentType("text/html;charset=utf-8");
			return view;
		} else {
			flash.setAttribute("user", user);
			JsonView view = new JsonView("success:false");
			view.setContentType("text/html;charset=utf-8");
			return view;
		}
	}

	/**
	 * 删除指定的账户
	 */
	public JsonView destroy(int id) throws Exception {
		User_list user = User_list.find(User_list.class, id);
		user.destroy();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	
	/**
	 *获得所有的角色
	 */
	public JsonView getSys_privs() throws Exception {
		List<Sys_priv> sysprivs = Sys_priv.findAll(Sys_priv.class); // 查询单位的一页资料
		JsonView view = new JsonView(sysprivs);
		System.out.println("获得所有的角色");
		System.out.println(view);
		// 构造JSON用的数据结构并返回JSON视图
		return view;
	}
}
